// void, Obj me

Unit this, u;
point pt;
bool sneakMode;
int i, l, t, time = 19000;
str names, hfu;

Sleep(rand(800)+200);
this = me.AsUnit();

while(!.Stop(1000));

while(1){
	if(.HasSpecial(sneak)){
		if(!.InHolder){
			if(.IsVisible){
				if(!.EnemiesInSight()){
					if(time > 9000) if(EnvReadString(.player, "Camouflage") == "researched")
						time = 9000;
						
					if(.TimeWithoutAttack >= time+rand(1000))
						.SetVisible(false);
				}
			}
		}
	}
	if(!.AI){
		if(sneakMode)
			sneakMode = false; 
		
		if(!.IsVisible){
			if(.hero.IsValid){	
				if(.hero.TimePastLastAttack() < GetConst("HeroTimePastLastAttack")){
					.SetLastAttackTime();
					.SetVisible(true);		 
				}
				names = "/1/Sneakmode/"+.hero.name;
				if(EnvReadString(names) != "yes")
					sneakMode = true;	
			}
		} 
	} else {
		if(!sneakMode)
			sneakMode = true;
	}
	if(!.InHolder)
    if(.IsVisible || sneakMode){
		if(.hero.IsValid){
			if(.hero.command == "stand_position"){
				Sleep(rand(100)+100);
				.AddCommand(true, "stand_position");
				return;
			}
		}
		u = .BestTargetInSquadSightMisZeroDamage;
		if(u.IsAlive()){
		if(.DistTo(u.pos) > .min_range){
			if(u.IsHeirOf("Catapult")){
				Sleep(rand(1000)+200);
			} else {
				bool bEngage = false;
				if(u.attack > 0) bEngage = true;
				else if(u.AsDruid.IsValid()) bEngage = true;
				else if(u.AsWagon.IsValid()) bEngage = true;
				
				if(u.hero.IsValid){
					if(u.hero.command == "retreat_notai" ||
						u.hero.command == "retreat_bld_notai")
						bEngage = false;
				}
				if(.hero.IsValid){
					if(u.IsHeirOf("BaseAnimal")){
						bEngage = false;
					}
				}
				if(bEngage){	
					Sleep(rand(200)+100);
					.AddCommand(true, "advance", .posRH);
					.AddCommand(true, "engage");
					return;
				}
			}
		} else {
			point p, pos;
			rect rc;
			
			if(u.attack > 0) {
				Sleep(rand(500)+500);
				rc = GetMapRect();
				pos.Set(0, .min_range+100);
				pos.Rot(rand(360));     
				p = pos + .pos; p.IntoRect(rc); 
				.AddCommand(true, "move", p); 
				return;
			}
		}
		}
	}
	{
		if(.Stop(3000)){
			//.pr("idle: doing nothing");
			if(!.AI){
				if(.IsValid)
					if(.IsMilitary)
						if(.speed_factor < 100)                                  
							.SetSpeedFactor(100);
				//zabranie artefaktu
				if(.IsValid){
					if(.IsMilitary){
						if(!.hero.IsValid){
							for (i = 0; i <= 10; i += 1){
								hfu = "HeroFavoriteUnit"+i;
								if(.HasItem(hfu))
									.RemoveItemsOfType(hfu);
							}
							if(.HasItem("Wings formation"))
								.RemoveItemsOfType("Wings formation");
						} else {
							if(.HasItem("Terrain bonus I"))
								.RemoveItemsOfType("Terrain bonus I");
							if(.HasItem("Terrain bonus II"))
								.RemoveItemsOfType("Terrain bonus II");
							if(.HasItem("Terrain bonus III"))
								.RemoveItemsOfType("Terrain bonus III");
							if(.HasItem("Terrain bonus IV"))
								.RemoveItemsOfType("Terrain bonus IV");
						}
					}
				}
            }                   
			if(.hero.IsValid){
				if(.hero.GetHolderSett.IsValid)
				if(!(.hero.GetHolderSett == .GetHolderSett))
				if(!.hero.GetHolderSett.GetCentralBuilding.IsHeirOf("BaseRuins"))
				if(EnvReadString(.hero.GetHolderSett, "autotrain") != "on"
					|| .inherentlevel >= EnvReadInt(.hero.GetHolderSett, "MaxAutotrainLevel")){
					Sleep(rand(200)+100);
					.AddCommand(true, "enter", .hero.GetHolderSett.GetCentralBuilding);
					return;
				}
			}
			if(.AI)
			if(!.hero.IsValid)
			if(!.InHolder)
			if(IsWaterLsa(GetGAIKA(this).LSA)){
				// leave the coast
				pt.Set(.pos.x + (150 - rand(300)), .pos.y + (150 - rand(300)));
				if(pt.InRect(GetMapRect))
				if(IsPassable3x3(pt))
				if(!IsWaterLsa(pt.GetGAIKA.LSA)){
					Sleep(rand(200)+100);
					.AddCommand(true, "move", pt);
					return;
				}
			}
				
			t = 2000;
			l+=1;
			if(l > 5)
				t = 4000;
				
			.Idle(t + rand(2000));
			
			if(rand(8)==0){
				pt = .GetDir();
				if(pt.x!=0 || pt.y!=0){
					pt.SetLen(100);
					pt.Rot(rand(60)-30);
					.Face(pt + .posRH);	
				}
			}
			while(.InHolder){
				.Idle(10000+rand(1000));
			} 
		}
	}
}